package io.gbznt.modules.buss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.gbznt.common.utils.Constant;
import io.gbznt.common.utils.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.Query;

import io.gbznt.modules.buss.dao.BussInspectionTaskDao;
import io.gbznt.modules.buss.entity.BussInspectionTaskEntity;
import io.gbznt.modules.buss.service.BussInspectionTaskService;
import io.gbznt.modules.sys.entity.SysDeptEntity;
import io.gbznt.modules.sys.entity.SysUserEntity;
import io.gbznt.modules.sys.service.SysDeptService;
import io.gbznt.modules.sys.service.SysUserService;


@Service("bussInspectionTaskService")
public class BussInspectionTaskServiceImpl extends ServiceImpl<BussInspectionTaskDao, BussInspectionTaskEntity> implements BussInspectionTaskService {

	@Autowired
	private SysUserService sysuser;
	@Autowired
	private SysDeptService sysdept;
	
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    	//搜索条件----名称---编号
    	String name = (String) params.get("name");
    	String bh = (String) params.get("bh");
    	//田块id
    	String farmlandId=(String)params.get("farmlandId");
        String status = (String)params.get("status");
        String isMe = (String) params.get("isMe");
    	
		IPage<BussInspectionTaskEntity> page = this.page(
                new Query<BussInspectionTaskEntity>().getPage(params),
                new QueryWrapper<BussInspectionTaskEntity>()
                .like(StringUtils.isNotBlank(name), "task_name", name)
                .like(StringUtils.isNotBlank(bh), "business_num", bh)
                .eq(StringUtils.isNotBlank(status), "status", status)
                .apply(StringUtils.isNotEmpty(isMe), "(create_id = {0} or handler = {0}) and status in (0,1)",
						ShiroUtils.getUserId(), ShiroUtils.getUserId())
                .like(StringUtils.isNotBlank(farmlandId), "farmland_info_ids", farmlandId)
				// 个人数据权限控制
				.in(!Constant.SUPER_ADMIN.equals(ShiroUtils.getUserId()),
						"dept_id", sysdept.getPermissionDeptIdsArr(true,true))
                .orderByDesc("create_time")
                
        );
        for (BussInspectionTaskEntity btask : page.getRecords()) {
			//巡检人
        	String xjr = btask.getHandler();
        	if(StringUtils.isNotBlank(xjr)){
        		SysUserEntity sysusere =  sysuser.getById(xjr);
        		if(sysusere!=null){
        			btask.setHandlername(sysusere.getName());
        		}
        	}
        	//部门id
        	String deptid = btask.getDeptId();
        	if(StringUtils.isNotBlank(xjr)){
        		SysDeptEntity sysusere =  sysdept.getById(deptid);
        		if(sysusere!=null){
        			btask.setDeptIdname(sysusere.getName());
        		}
        	}
        	
        	
		}
        
        return new PageUtils(page);
    }

}
